package xxm.utility.main;

import java.net.DatagramSocket;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.DatagramChannel;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.util.Iterator;
import java.util.concurrent.Semaphore;

import xxm.utility.buffer.BasicUdpRemoteRepBuffer;
import xxm.utility.event.BasicNetEvent;
import xxm.utility.event.BasicNetEventListener;
import xxm.utility.pack.BasicPackage;
import xxm.utility.socket.BasicSocketEvent;
import xxm.utility.util.BasicLog;


public class Main implements BasicNetEventListener{

	private Semaphore semaphore = new Semaphore(0);
	private BasicSocketEvent socket = null;
	public static void main(String[] args) {
		System.out.println(Byte.SIZE);
		System.out.println(Integer.SIZE);
		System.out.println(Character.SIZE);
		new Main();
		
	}
	
	class ThreadOne implements Runnable {

		/* (non-Javadoc)
		 * @see java.lang.Runnable#run()
		 */
		@Override
		public void run() {
			// TODO Auto-generated method stub
			/*try {
				
				while( true ) {
					semaphore.acquire();
					System.out.println("ThreadOne");
				}
				//semaphore.release();

			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}*/
		}
		
	}
	
	class ThreadTow implements Runnable {

		/* (non-Javadoc)
		 * @see java.lang.Runnable#run()
		 */
		@Override
		public void run() {
			
			//BasicLog.setLogLevel(0);
			// TODO Auto-generated method stub
			BasicUdpRemoteRepBuffer buffer = new BasicUdpRemoteRepBuffer();
			buffer.RunRepDispenseThread();
			try {
				Thread.sleep(1000);
			} catch (InterruptedException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			BasicPackage pack = new BasicPackage();
			buffer.addRepPackage(pack);
			buffer.addRepPackage(pack);
			buffer.addRepPackage(pack);
			buffer.addRepPackage(pack);
			buffer.addRepPackage(pack);
			

			buffer.StopRepDispenseThreaad(1);
			
			
		}
		
	}
	
	public Main() {
		
		Thread th1 = new Thread(new ThreadTow());
		Thread th2 = new Thread(new ThreadOne());
		
		th1.start();
		th2.start();
		int n = 60;
		
		
		
		try {
			th1.join();
			th2.join();
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		/*while( --n > 0 ) {
			try {
				Thread.sleep(1);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}*/
		
		/*BasicPackageHeader header = new BasicPackageHeader();
		header.setSign( (byte)0x01 );					// 1 byte
		header.setPackageID( 2 );			// 4 byte
		header.setPackageTotal((byte)0x03);			// 1 byte
		header.setPackageSerial((byte)0x04);			// 1 byte
		header.setcCrc( 5 );				// 4 byte
		
		byte[] buf = new byte[11];
		header.toBuffer(buf, true);
		
		ByteBuffer buffer = ByteBuffer.wrap(buf);
		buffer.order(ByteOrder.LITTLE_ENDIAN);
		buffer.clear();
		int b = buffer.get();
		buffer.clear();
		b = buffer.get();
		
		header.formatBuffer(buf, true);*/
		
		/*ClientRecv t1 = new ClientRecv();
		ClientSend t2 = new ClientSend();
		new Thread(t1).start();
		new Thread(t2).start();
		
		int n = 10;
		
		while( --n > 0 ) {
			try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		t1.bRun = false;
		t2.bRun = false;*/
		/*try {
			DatagramChannel recvhannel = DatagramChannel.open();
			recvhannel.configureBlocking(false);
			DatagramSocket socket = recvhannel.socket();
			socket.bind(new InetSocketAddress("127.0.0.1", 80));
			Selector selector = Selector.open();
			recvhannel.register(selector, SelectionKey.OP_READ);
			
			int b  = 00 ;
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}*/
		
		/*try {
			socket = new BasicSocketEvent();
			socket.addBasicNetEventListener(this);
			DatagramPacket p = null;
			socket.send(p);
			SocketChannel soc;
		} catch (Exception e) {
			// TODO: handle exception
		}
		java.lang.Thread th;
		
		BasicPackageHeader header = new BasicPackageHeader();
		header.setSign( (byte)0x01 );					// 1 byte
		header.setPackageID( 2 );			// 4 byte
		header.setPackageTotal((byte)0x03);			// 1 byte
		header.setPackageSerial((byte)0x04);			// 1 byte
		header.setcCrc( 5 );				// 4 byte
		
		byte[] buf = new byte[11];
		header.toBuffer(buf, true);
		
		header.formatBuffer(buf, true);
		
		byte []bb = null;
		header.formatBuffer(bb, false);
		
		int var = header.getPackageID();
		
		int b = 000;
		*/
	}

	@Override
	public void actionPerformed(BasicNetEvent e) {
		// TODO Auto-generated method stub
		if( e.getSource() == socket ) {
			int b = 0;
			b = 1;
		}
	}
	
	class ClientSend implements Runnable {

		public boolean bRun = true;
		@Override
		public void run() {
			// TODO Auto-generated method stub
			try {
				System.out.println("ClientSend");
				DatagramChannel sendhannel = DatagramChannel.open();
				sendhannel.configureBlocking(false);
				
				sendhannel.connect(new InetSocketAddress("127.0.0.1", 5555));
				ByteBuffer buf = ByteBuffer.allocate(1024);
				buf.putInt(1);
				buf.putInt(2);
				buf.putInt(3);
				buf.putInt(4);
				buf.putInt(5);
				while( bRun ) {
					sendhannel.write(buf);
					Thread.sleep(3000);
					System.out.println(" ClientSend next ");
				}
				
				sendhannel.close();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			
		}
		
	}
	
	class ClientRecv implements Runnable {

		public boolean bRun = true;
		@Override
		public void run() {
			// TODO Auto-generated method stub
			try {
				System.out.println("ClientRecv");
				DatagramChannel recvhannel = DatagramChannel.open();
				recvhannel.configureBlocking(false);
				DatagramSocket socket = recvhannel.socket();
				socket.bind(new InetSocketAddress("127.0.0.1", 5555));
				Selector selector = Selector.open();
				
				recvhannel.register(selector, SelectionKey.OP_READ);
				
				ByteBuffer buf= ByteBuffer.allocate(1024);
				while( bRun ) {
					
					if( selector.select(1000) == 0 ){
						continue;
					}
					Iterator<SelectionKey> it = selector.selectedKeys().iterator();
					
					SelectionKey key = (SelectionKey)it.next();
					
					if( key.isReadable() ) {
						recvhannel.read(buf);
						System.out.println(buf.getInt());
						System.out.println(buf.getInt());
						System.out.println(buf.getInt());
						System.out.println(buf.getInt());
						System.out.println(buf.getInt());
					}
					System.out.println(" ClientRecv next ");
					
				}
				
				selector.close();
				recvhannel.close();
				
				int b = 0;
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
		}
		
	}
}
